<template>
  <div class="login_container">
      <el-form :model="loginForm" :rules="rules" ref="loginForm" class="login_form">
         <div class="title_container">
            <h3 class="title">Login Form</h3>
         </div>
         <el-form-item prop="name">
           <el-input v-model="loginForm.username" prefix-icon="el-icon-user-solid" placeholder="请输入用户名"></el-input>
         </el-form-item>

          <el-tooltip v-model="capsTooltip" content="Caps lock is On" placement="right" manual>
             <el-form-item  prop="password">
               <el-input
               :type="passwordType"
               ref="password"
               v-model="loginForm.password"
               prefix-icon="el-icon-lock"
               placeholder="请输入密码"
               @keyup.native="checkCapslock"
               @blur="capsTooltip = false"
               @keyup.enter.native="submitForm"
               >
                  <i slot="suffix" @click="showPwd" :class="passwordType === 'password' ? 'el-icon-open' : 'el-icon-turn-off'"></i>
               </el-input>
             </el-form-item>
          </el-tooltip>

          <el-form-item>
            <el-button :loading="loading" type="primary" @click="submitForm('loginForm')">登录</el-button>
          </el-form-item>
      </el-form>
  </div>
</template>

<script>
export default {
  name: 'login',
  data () {
    return {
      capsTooltip: false, // 是否显示大写提示语
      loading: false,
      redirect: undefined,
      otherQuery: {},
      passwordType: 'password',
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }

        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query
        console.log('route', route)
        if (query) {
          this.redirect = query.redirect
          this.otherQuery = this.getOtherQuery(query)
        }
      },
      immediate: true
    }
  },
  mounted () {
    if (this.loginForm.username === '') {
      this.$refs.username.focus()
    } else if (this.loginForm.password === '') {
      this.$refs.password.focus()
    }
  },
  methods: {
    // 输入密码是提示大写是否开启
    checkCapslock (e) {
      const { key } = e
      this.capsTooltip = key && key.length === 1 && (key >= 'A' && key <= 'Z')
    },
    submitForm () {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(() => {
              this.$router.push({ path: this.redirect || '/', query: this.otherQuery })
            })
        } else {
          console.log('error submit!!')
          return false
        }
        // this.$nextTick(() => {
        //   this.$refs.password.focus()
        // })
      })
    },
    showPwd () {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    getOtherQuery (query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== 'redirect') {
          acc[cur] = query[cur]
        }
        return acc
      }, {})
    }
  }

}
</script>

<style lang="less" scoped>

.login_container{
    width: 100vw;
    height: 100vh;
    background-color: #2d3a4b;
    .login_form{
        width: 420px;
        margin: 0 auto;
        // transform: translateY(50%);
        padding: 160px 35px 0;
        .title_container{
          margin-bottom: 20px;
            .title{
             color: #fff;
            text-align: center;
            font-weight: bold;
            margin: 0 auto;
            }
        }
        .el-button--primary{
            width: 100%;
        }
        /deep/.el-input__suffix-inner{
          font-size: 24px;
          padding-right: 6px;
        }

      }
  }

</style>
